package com.bzr.emos.wx.tenoss;
import java.io.File;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PutObject {


    @Value("${ten.SecretId}")
    private String secretId;
    @Value("${ten.SecretKey}")
    private String secretKey;
    @Value("${ten.BucketName}")
    private String bucketName;
    @Value("${ten.FilePath}")
    private String filePath;




    public String putTxOss(String fileName , File file) {
        try {
            // 初始化用户身份信息(secretId, secretKey)
            COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
            // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
            ClientConfig clientConfig = new ClientConfig(new Region("ap-nanjing"));
            // 生成cos客户端
            COSClient cosClient = new COSClient(cred, clientConfig);
            String key = filePath+fileName;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            return "https://"+bucketName+".cos.ap-nanjing.myqcloud.com/"+filePath+fileName;
        } catch (CosClientException e) {
            return null;
        }



    }
}